package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 自定义切面，实现自动填充字段功能
 */
@Slf4j
@Component
@Aspect
public class AutoFillAspect {

    //切面切入点
    @Pointcut(value = "execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillAspect(){};

    @Before("autoFillAspect()")
    public void atuoFill(JoinPoint joinPoint) throws NoSuchMethodException {
        log.info("自定义切面开始执行......");
        //获取当前被拦截方法的数据库操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法签名对象
        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);//获取方法上的注解对象
        OperationType operationType = annotation.value();// 获取注解对象中的 value 属性值

        //获取挡墙被拦截方法的方法参数————实体对象
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            //参数为空
            return;
        }
        Object entity = args[0];

        //准备赋值参数

        Long currentId = BaseContext.getCurrentId();

        //通过反射为实体对象赋值
        if (operationType == OperationType.INSERT){
            // 数据库操作类型为 INSERT
            Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
            Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
            Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
            Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

            try {
                setCreateTime.invoke(entity,LocalDateTime.now());
                setCreateUser.invoke(entity,currentId);
                setUpdateTime.invoke(entity,LocalDateTime.now());
                setUpdateUser.invoke(entity,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if (operationType == OperationType.UPDATE){
            //数据库操作类型为 UPDATE
            Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
            Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

            try {
                setUpdateTime.invoke(entity,LocalDateTime.now());
                setUpdateUser.invoke(entity,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
